﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using BO.Infrastructure.Utils;
using Utilities.Entities;

namespace BO.ProjectManager
{
    public class PMBO_NoCache
    {
        private const string ExtensionSourceSetsFilePath = "/App_Data/cssAndJsConfig.xml";

        internal static List<ExtensionSourceSet> GetExtensionSourceSets(out string fileDependency)
        {
            var filePath = HttpContext.Current.Server.MapPath(ExtensionSourceSetsFilePath);
            if (File.Exists(filePath))
            {
                var doc = XElement.Load(filePath);

                var useStorage = ((string)doc.Attribute("useStorage")).Equals("true", StringComparison.InvariantCultureIgnoreCase);
                var storageDomain = (string)doc.Attribute("storageDomain");
                var storageVersion = (string)doc.Attribute("storageVersion");

                var pages = doc.Descendants("page");
                var enumerable = pages as XElement[] ?? pages.ToArray();
                if (enumerable.Any())
                {

                    var result = new List<ExtensionSourceSet>();
                    foreach (var page in enumerable)
                    {

                        //url
                        var url = page.Attribute("url") != null ? (string)page.Attribute("url") : string.Empty;

                        //js
                        var jsSourceSet = new string[] { };
                        {
                            var js = page.Descendants("js");
                            var xElements = js as XElement[] ?? js.ToArray();
                            if (xElements.Any())
                            {

                                var query = from jsItem in xElements

                                            let isCombres =
                                                jsItem.Attribute("isCombres") != null &&
                                                (bool)jsItem.Attribute("isCombres")

                                            let source =
                                            isCombres && useStorage
                                             ? string.Format("{0}/{1}_{2}.js", storageDomain, (string)jsItem.Attribute("src"), storageVersion)
                                             : jsItem.Attribute("src") != null
                                                        ? isCombres
                                                                ? Combres.WebExtensions.CombresUrl((string)jsItem.Attribute("src"))
                                                                : (string)jsItem.Attribute("src")
                                                        : string.Empty
                                            select source;

                                jsSourceSet = query.ToArray();



                            }
                        }

                        //css
                        var cssSourceSet = new string[] { };
                        {
                            var css = page.Descendants("css");
                            var xElements = css as XElement[] ?? css.ToArray();
                            if (xElements.Any())
                            {
                                var query = from cssItem in xElements

                                            let isCombres =
                                                cssItem.Attribute("isCombres") != null &&
                                                (bool)cssItem.Attribute("isCombres")

                                            let source =
                                            isCombres && useStorage
                                             ? string.Format("{0}/{1}_{2}.css", storageDomain, (string)cssItem.Attribute("src"), storageVersion)
                                             : cssItem.Attribute("src") != null
                                                        ? isCombres
                                                                ? Combres.WebExtensions.CombresUrl((string)cssItem.Attribute("src"))
                                                                : (string)cssItem.Attribute("src")
                                                        : string.Empty

                                            select source;

                                cssSourceSet = query.ToArray();
                            }
                        }

                        result.Add(new ExtensionSourceSet
                            {
                                Url = url,
                                JsSourceSet = jsSourceSet,
                                CssSourceSet = cssSourceSet
                            });
                    }
                    fileDependency = filePath;
                    return result;
                }
            }
            fileDependency = string.Empty;
            return null;
        }

        internal static StorageConfig GetCurrentStorageConfig(out string fileDependency)
        {
            var path = HttpContext.Current.Server.MapPath("/App_Data/cssAndJsConfig.xml");
            if (File.Exists(path))
            {
                var doc = new XmlDocument();
                doc.Load(path);
                var element = doc.GetElementsByTagName("pages")[0] as XmlElement;
                if (element != null)
                {
                    fileDependency = path;
                    return new StorageConfig
                        {
                            Domain = element.GetAttribute("storageDomain"),
                            IsUsed = PresentationUtils.Object2Boolean(element.GetAttribute("useStorage")),
                            DefaultVersion = PresentationUtils.Object2Integer(element.GetAttribute("storageVersion"))
                        };
                }
            }
            fileDependency = string.Empty;
            return null;
        }
    }
}
